# 这里是封装的断言
# 如果要进行断言，可调用assert_compare方法

from utils.log_util import logger


def assert_compare(expect, compare, actual):
    """

    :param expect: 预期结果
    :param compare: 断言方式
    :param actual: 实际结果
    :return: 
    """
    logger.info(f"预期结果：{expect} {compare} {actual}")
    try:  # 异常机制,条件判断，如果条件成立，执行下面缩进部分的代码
        if compare == "==":
            assert expect == actual
        elif compare == "!=":
            assert expect != actual
        elif compare == ">":
            assert expect >= actual
        elif compare == "<=":
            assert expect <= actual
        elif compare == "in":
            assert expect in actual
        else:  # 条件不成立时执行else下面部分的代码
            try:
                raise NameError(f"{compare}断言方式错误，请填写正确")   #对异常抛出
            except Exception as e:
                logger.error(e)
                raise

        logger.info("断言成功")
    except AssertionError as e:  # 抓取AssertionError，如果断言不通过抓取异常存放到日志中
        logger.error(f"断言失败{e}")
        raise  # 把捕获的异常AssertionError再次进行抛出
